home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / _dup.c < prev    next >
C/C++ Source or Header  |  1994-04-12  |  3KB  |  121 lines

  1. RCS_ID_C="$Id: _dup.c,v 3.1 1994/04/09 13:25:55 ppessi Exp $";
  2. /*
  3.  * _dup.c - duplicate a file descriptor for SAS C 
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Thu Mar 24 05:19:36 1994 ppessi
  12.  * Last modified: Thu Mar 24 07:27:30 1994 ppessi
  13.  *
  14.  */
  15.  
  16. #include <ios1.h>
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19. #include <dos.h>
  20. #define USE_BUILTIN_MATH
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <dos/dos.h>
  24. #include <proto/dos.h>
  25.  
  26. #include <bsdsocket.h>
  27.  
  28. /****** net.lib/dup ***********************************************************
  29.  
  30.     NAME
  31.         dup, dup2 - duplicate an existing file descriptor
  32.  
  33.     SYNOPSIS
  34.         #include <unistd.h>
  35.  
  36.         int dup(int oldd)
  37.  
  38.         int dup2(int oldd, int newd)
  39.  
  40.     FUNCTION
  41.         Dup() duplicates an existing object descriptor and returns its value
  42.         to the calling program (newd = dup(oldd)). The argument oldd is a
  43.         small nonnegative integer index in the program's descriptor table.
  44.         The value must be less than the size of the table, which is returned
  45.         by getdtablesize().  The new descriptor returned by the call is the
  46.         lowest numbered descriptor currently not in use by the program.
  47.  
  48.         The object referenced by the descriptor does not distinguish between
  49.         oldd and newd in any way.  Thus if newd and oldd are duplicate
  50.         references to an open file, read() and write() calls all move a single
  51.         pointer into the file, and append mode, non-blocking I/O and
  52.         asynchronous I/O options are shared between the references.  If a
  53.         separate pointer into the file is desired, a different object
  54.         reference to the file must be obtained by issuing an additional open()
  55.         call.  The close-on-exec flag on the new file descriptor is unset.
  56.  
  57.         In dup2(), the value of the new descriptor newd is specified.  If this
  58.         descriptor is already in use, the descriptor is first deallocated as
  59.         if a close() call had been done first.
  60.  
  61.     RETURN VALUES
  62.         The value -1 is returned if an error occurs in either call.  The
  63.         external variable errno indicates the cause of the error.
  64.  
  65.     BUGS
  66.         The current UFB implementation for SAS C allows only sockets to be
  67.         duplicated.
  68.  
  69.     ERRORS
  70.         Dup() and dup2() fail if:
  71.  
  72.         [EBADF]       Oldd or newd is not a valid active descriptor
  73.  
  74.         [EMFILE]      Too many descriptors are active.
  75.  
  76.     SEE ALSO
  77.         accept(),  open(),  close(),  socket(),  getdtablesize()
  78.  
  79.     STANDARDS
  80.         Dup() and dup2() are expected to conform to IEEE Std 1003.1-1988
  81.         (``POSIX'').
  82.  
  83.     COPYRIGHT
  84.         This manual page is copyright © 1980, 1991 Regents of the
  85.         University of California.  All rights reserved.
  86.  
  87. *******************************************************************************
  88. */
  89.  
  90.  
  91. int
  92. dup(int old_fd)
  93. {
  94.   struct UFB *ufb;
  95.   int ufbflg;
  96.   /*
  97.    * Check for the break signals
  98.    */
  99.   __chkabort();
  100.  
  101.   /*
  102.    * Find the ufb * for the given FD
  103.    */
  104.   if ((ufb = __chkufb(old_fd)) == NULL) {
  105.     errno = EBADF;
  106.     return -1;
  107.   }
  108.   
  109.   ufbflg = ufb->ufbflg;
  110.  
  111.   /* 
  112.    * The brain dead UFB system won't allow duplicating ordinary files
  113.    */
  114.   if ((ufbflg & UFB_SOCK) == UFB_SOCK) {
  115.     return Dup2Socket(old_fd, -1);
  116.   } else {
  117.     errno = EBADF;
  118.     return -1;
  119.   }
  120. }
  121.